<template>
  <d2-container>
    <!-- 角色管理主页面 -->
    <el-table :data="tableData" style="width: 100%" stripe size="mini">
      <el-table-column prop="ruleName" label="角色名称"></el-table-column>
      <el-table-column prop="ruleDesc" label="角色描述"></el-table-column>
      <!-- 右侧按钮 -->
      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-button type="primary" plain size="mini" @click="onAddRule">新增</el-button>
        </template>
        <template slot-scope="scope">
          <el-button size="mini" type="success" plain @click="onUpdateRule(scope.row)">修改</el-button>
          <el-button size="mini" type="success" plain @click="showModifyMenuDialog(scope.row)">分配菜单</el-button>
          <el-button size="mini" type="danger" @click="onDelRule(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <template slot="footer">
      <el-pagination background layout="prev, pager, next" :total="1000"></el-pagination>
    </template>

    <!-- 弹出框 -->
    <!-- <el-dialog title="提示" :visible.sync="showModify" width="30%" center>
      <el-form ref="form" :model="formData" label-width="80px" :rules="validateRules">
        <el-form-item label="角色名称" prop="ruleName">
          <el-input
            v-model="formData.ruleName"
            placeholder="请输入角色名称"
            maxlength="50"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input
            v-model="formData.ruleDesc"
            type="textarea"
            :rows="2"
            placeholder="请输入角色描述"
            maxlength="100"
            show-word-limit
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onModify">确定</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog> -->

    <!-- 穿梭框 -->
    <el-dialog title="提示" :visible.sync="showModifyMenu" width="550px" center>
      <el-transfer v-model="selectedMenuList" :data="menuData" :titles="['待选菜单','已选菜单']"></el-transfer>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showModifyMenu = false">取 消</el-button> 
        <!-- 穿梭框消失 -->
        <el-button type="primary" @click="onModifyMenu">确 定</el-button>
      </span>
    </el-dialog> 
  </d2-container>
</template>


<script>
export default {
  data() {
    return {
      showModifyMenu: false,
      formData: {},
      showModify: false,
      tableData: [
        { id: "1", ruleName: "收银员", ruleDesc: "收钱的" },
        { id: "2", ruleName: "财务", ruleDesc: "数钱的" },
        { id: "3", ruleName: "物业经理", ruleDesc: "啥都不干" },
        { id: "4", ruleName: "财务经理", ruleDesc: "啥都不干" }
      ],
      menuData: [
        { key: "1", label: "角色管理" },
        { key: "2", label: "菜单管理" },
        { key: "3", label: "用户管理" },
      ],
      selectedMenuList: [], //把挑选的menuData放在这里
      currentModifyMenuRow: {}, 
      validateRules: {
        ruleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" }
        ]
      }
    };
  },

  methods: {
    onAddRule() {
      this.showModify = true;
    },
    onUpdateRule(row) {
      console.log(row);
      this.formData = row;
      this.showModify = true;
    },

    onDelRule() {
      this.$confirm("此操作将永久删除该信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    onModify() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.formData.id) {
            console.log("这是修改方法,调用修改方法");
          } else {
            console.log("这是新增方法");
          }
          this.showModify = false;
        } else {
          console.log("验证失败");
        }
      });
    },

    //分配菜单
    showModifyMenuDialog(row) {        //row获得信息  是哪一条数据的分配菜单按钮触发的事件  
      console.log(row)
      this.showModifyMenu = true;
      this.currentModifyMenuRow = row  //让弹出的穿梭框获得这条数据   是哪个人在分配角色
    },

    //分配完菜单后确定提交 
    onModifyMenu(){
      this.showModifyMenu = false;
      console.log(this.selectedMenuList);      //提交的时候 获得挑选出来的数据   给这个人分配的角色是哪些
      console.log(this.currentModifyMenuRow)   //是哪个人在分配角色
    },

  }
};
</script>